package com.kitty.game.welfare.service;

import com.kitty.common.cache.RedisService;
import com.kitty.game.reward.model.RewardUnitSet;
import com.kitty.game.role.model.Role;
import com.kitty.game.reward.model.RewardSet;
import com.kitty.game.reward.service.RewardService;
import com.kitty.game.utils.Const;
import com.kitty.game.welfare.WelfareDataPool;
import com.kitty.game.welfare.message.RespLoginGift;
import com.kitty.game.welfare.message.vo.LoginReward;
import com.kitty.mina.cache.DataCache;
import com.kitty.mina.message.MessagePusher;
import com.kitty.common.utils.DecodeSendMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**七日登录*/
@Component
public class ServenDayRewardHandler {
    @Autowired
    RewardService rewardService;

    @Autowired
    RedisService redisService;

    public void load(){
        loadCommonSet();
    }
    public void loadCommonSet(){
        List<RewardSet> rewardSetList = new ArrayList<>();


        for (int i = 0; i <7 ; i++) {
            RewardSet rewardSet = new RewardSet();
            List<RewardUnitSet> list = new ArrayList<>();
            String serven_day_reward = DataCache.REDIS_DATA.get("serven_day_reward_"+(i+1));
            String[] split = serven_day_reward.split("\\+");
            for (String s : split) {
                RewardUnitSet rewardUnitSet  = new RewardUnitSet();

                String type = s.split("-")[0];
                String name = s.split("-")[1];
                if("物品".equals(type)){
                    rewardUnitSet.setBind((byte)1);
                    rewardUnitSet.setType((byte)1);
                    rewardUnitSet.setShowName(name);
                    rewardUnitSet.setName(name);
                    rewardUnitSet.setCount(Integer.parseInt(DecodeSendMessage.getParam(s.split("-")[2],2)));
                }else if("宠物".equals(type)){
                    rewardUnitSet.setBind((byte)1);
                    String petName = s.split("-")[2];
                    rewardUnitSet.setType((byte)5);
                    rewardUnitSet.setShowName(petName+"("+name+")");
                    rewardUnitSet.setName(petName);
                    rewardUnitSet.setCount(1);
                }else if("金元宝".equals(type)){
                    int count =  Integer.parseInt(DecodeSendMessage.getParam(name,2));
                    rewardUnitSet.setType((byte)14);
                    rewardUnitSet.setShowName(type);
                    rewardUnitSet.setName(type);
                    rewardUnitSet.setCount(count);
                }else if("银元宝".equals(type)){
                    int count =  Integer.parseInt(DecodeSendMessage.getParam(name,2));
                    rewardUnitSet.setType((byte)15);
                    rewardUnitSet.setShowName(type);
                    rewardUnitSet.setName(type);
                    rewardUnitSet.setCount(count);
                }
                list.add(rewardUnitSet);
            }
            rewardSet.setList(list);
            rewardSetList.add(rewardSet);;
        }
        WelfareDataPool.servenDayRewardSets = rewardSetList;
        Map<Byte, RewardSet> day2RewardSetMap = new HashMap<>();
        byte day = 0;
        for (int i= 0; i<rewardSetList.size(); i++) {
            day = (byte) (i+1);
            RewardSet rewardSet2 = rewardSetList.get(i);
            day2RewardSetMap.put(day, rewardSet2);
        }
        WelfareDataPool.day2RewardSetMap = day2RewardSetMap;
    }


    /**打开福利七日登录奖励界面*/
    public void open(Role role) {
        RespLoginGift respLoginGift = getRespLoginGift(role);

        MessagePusher.pushMessage(role, respLoginGift);
    }

    private RespLoginGift getRespLoginGift(Role role) {
        RespLoginGift respLoginGift = new RespLoginGift();

        byte loginDays = (byte) (role.getLoginDays() > WelfareDataPool.servenDayRewardSets.size() ? WelfareDataPool.servenDayRewardSets.size() : role.getLoginDays());
        respLoginGift.setLoginDays(loginDays);
        List<LoginReward> rewards = new ArrayList<LoginReward>();
        respLoginGift.setRewards(rewards);

        byte index = 0;
        byte day = 0;
        for (RewardSet servenDayRewardSet : WelfareDataPool.servenDayRewardSets) {
            LoginReward loginReward = new LoginReward();

            loginReward.setIndex(index);
            day = (byte) (index + 1);
            byte flag = (byte) (role.getActivity().getServenDayRewardStatus().getOrDefault(day, false) == true ? 1 : 0);
            loginReward.setFlag(flag);
            loginReward.setDesc(servenDayRewardSet.getDesc());

            rewards.add(loginReward);

            index += 1;
        }

        return respLoginGift;
    }

    private boolean isFetch(Role role, byte day) {
        return role.getActivity().getServenDayRewardStatus().getOrDefault(day, false);
    }

    /**领取奖励*/
    public void fetchLoginGift(Role role, byte index) {
        byte day = (byte) (index + 1);
        if (day > 0) {
            RewardSet servenDayRewardSet = WelfareDataPool.day2RewardSetMap.get(day);
            if (servenDayRewardSet == null) {return ;}
            /**判断是否已经领取*/
            if (isFetch(role, day)) {return ;}
            /**判断是否可以领取*/
            if (day > role.getLoginDays()) {return ;}
            /**判断能否给奖励*/
            if (!rewardService.checkAddReward(role, servenDayRewardSet)) {
                return ;
            }


            /**设置已经领取状态*/
            role.getActivity().getServenDayRewardStatus().put(day, true);
            rewardService.addReward(role, servenDayRewardSet);
         //   LoggerFunction.ACTIVITY.getLogger().info("{}({})fetchLoginGift day:{}", role.getUid(), role.getName(), day);

            RespLoginGift respLoginGift = getRespLoginGift(role);
            MessagePusher.pushMessage(role, respLoginGift);
        }
    }

    public static void main(String[] args) {
        long dailyFiveResetTime =1651460952535L;
        Calendar roleResetCalader = Calendar.getInstance();
        roleResetCalader.setTimeInMillis(dailyFiveResetTime);
        System.out.println(roleResetCalader.get(Calendar.HOUR_OF_DAY));

        /** 玩家上一重置时间在重置时间之前的，则重置 */
        Calendar systemResetTime = getSystemFiveResetTime();
        System.out.println(systemResetTime.get(Calendar.HOUR_OF_DAY));
        System.out.println(roleResetCalader.before(systemResetTime));
    }
    /**
     * 获得系统5点重置的时间
     */
    public static Calendar getSystemFiveResetTime() {
        Calendar calendar = Calendar.getInstance();
        /**当前小时小于重置的小时，拿前一天*/
        if (calendar.get(Calendar.HOUR_OF_DAY) < Const.RESET_HOUR) {
            calendar.add(Calendar.DAY_OF_YEAR, -1);
        }
        calendar.set(Calendar.HOUR_OF_DAY, Const.RESET_HOUR);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        return calendar;
    }
}
